package com.cy.pj.common.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

//@Component
@Aspect
public class SysCacheAspect {

    private Map<String,Object> cache=new ConcurrentHashMap<>();
    //@annotation
    @Pointcut("@annotation(com.cy.pj.common.annotation.RequiredCache)")//细粒度切入点表达式
    public void doCache(){}

    @Pointcut("@annotation(com.cy.pj.common.annotation.ClearCache)")
    public void ClearCache(){}
    @AfterReturning("ClearCache()")
    public void doAfterReturning(){
        cache.clear();
    }
    @Around("doCache()")
    public Object doCacheAround(ProceedingJoinPoint joinPoint)throws Throwable{
        System.out.println("Get Data From Cache");
        Object result=cache.get("deptkey");
        if (result!=null)return result;
        result=joinPoint.proceed();
        System.out.println("Put data to cache");
        cache.put("deptkey", result);
        return result;
    }

}
